﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AppLab.Web;

namespace AppLab.Controls
{
    public class ReviewCustomDataForm : CustomDataForm
    {
        protected override void OnAutoGeneratingField(DataFormAutoGeneratingFieldEventArgs e)
        {

            if (e.PropertyName == "MoblieBrandAndType")
            {
                e.Field.ReplaceTextBox(new TextBlock(), TextBlock.TextProperty, null);
            }
            else if (e.PropertyName == "SubmitBy")
            {
                e.Field.ReplaceTextBox(new TextBlock(), TextBlock.TextProperty, null);
            }
            else if (e.PropertyName == "SubmitTimeString")
            {
                e.Field.ReplaceTextBox(new TextBlock(), TextBlock.TextProperty, null);//bingding=>bingding.Converter = new DateTimeConverter());
            }
            else if (e.PropertyName == "SubmitType")
            {
                e.Field.ReplaceTextBox(new TextBlock(), TextBlock.TextProperty, null);
            }
            else if (e.PropertyName == "SubmitComment")
            {
                TextBlock txBlock = new TextBlock();
                txBlock.Width = 255;
                txBlock.TextWrapping = TextWrapping.Wrap;
                e.Field.ReplaceTextBox(txBlock, TextBlock.TextProperty, null);
            }
            else if (e.PropertyName == "SubmitPackage")
            {
                e.Field.ReplaceTextBox(new DownloadLink(GetDataContextGuid(), "SubmitPackage"), DownloadLink.ContentProperty, null);
            }
            else if (e.PropertyName == "SubmitUpgradePackage")
            {
                if (GetEnableUpgradePackage())
                {
                    e.Field.ReplaceTextBox(new DownloadLink(GetDataContextGuid(), "SubmitUpgradePackage"), DownloadLink.ContentProperty, null);
                }
                else
                {
                    e.Field.ReplaceTextBox(new TextBlock(), TextBlock.TextProperty, null);
                }
            }
            else if (e.PropertyName == "SubmitReleaseNote")
            {
                if (this.GetEnableSubmitReleaseNote())
                {
                    e.Field.ReplaceTextBox(new DownloadLink(GetDataContextGuid(), "SubmitReleaseNote"), DownloadLink.ContentProperty, null);
                }
                else
                {
                    e.Field.ReplaceTextBox(new TextBlock(), TextBlock.TextProperty, null);
                }
            }
            else if (e.PropertyName == "SubmitSelfTest")
            {
                if (GetEnableSubmitSelfTest())
                {
                    e.Field.ReplaceTextBox(new DownloadLink(GetDataContextGuid(), "SubmitSelfTest"), DownloadLink.ContentProperty, null);
                }
                else
                {
                    e.Field.ReplaceTextBox(new TextBlock(), TextBlock.TextProperty, null);
                }
            }
            else if (e.PropertyName == "SubmitAnythingElse")
            {
                if (GetEnableAnythingElse())
                {
                    e.Field.ReplaceTextBox(new DownloadLink(GetDataContextGuid(), "SubmitAnythingElse"), DownloadLink.ContentProperty, null);
                }
                else
                {
                    e.Field.ReplaceTextBox(new TextBlock(), TextBlock.TextProperty, null);
                }
            }
            else if (e.PropertyName == "InspectionReport")
            {
                if (GetEnableSpectionReport())
                {
                    e.Field.ReplaceTextBox(new DownloadLink(GetDataContextGuid(), "InspectionReport"), DownloadLink.ContentProperty, null);
                }
                else
                {
                    e.Field.ReplaceTextBox(new TextBlock(), TextBlock.TextProperty, null);
                }
            }
           // base.OnAutoGeneratingField(e);
        }

        /// <summary>
        /// 自测报告
        /// </summary>
        /// <returns></returns>
        private bool GetEnableSubmitSelfTest()
        {
            if (this.CurrentItem != null)
            {
                HistoryData history = this.CurrentItem as HistoryData;
                if (history != null)
                {
                    return !string.IsNullOrEmpty(history.SubmitSelfTest);
                }

                ReviewSubmitData review = this.CurrentItem as ReviewSubmitData;
                if (review != null)
                {
                    return !string.IsNullOrEmpty(review.SubmitSelfTest);
                }

                TaskSubmitData task = this.CurrentItem as TaskSubmitData;
                if (task != null)
                {
                    return !string.IsNullOrEmpty(task.SubmitSelfTest);
                }
            }

            return false;
        }

        /// <summary>
        /// ReleaseNote
        /// </summary>
        /// <returns></returns>
        private bool GetEnableSubmitReleaseNote()
        {
            if (this.CurrentItem != null)
            {
                HistoryData history = this.CurrentItem as HistoryData;
                if (history != null)
                {
                    return !string.IsNullOrEmpty(history.SubmitReleaseNote);
                }

                ReviewSubmitData review = this.CurrentItem as ReviewSubmitData;
                if (review != null)
                {
                    return !string.IsNullOrEmpty(review.SubmitReleaseNote);
                }

                TaskSubmitData task = this.CurrentItem as TaskSubmitData;
                if (task != null)
                {
                    return !string.IsNullOrEmpty(task.SubmitReleaseNote);
                }
            }

            return false;
        }

        /// <summary>
        /// 升级包
        /// </summary>
        /// <returns></returns>
        private bool GetEnableUpgradePackage()
        {
            if (this.CurrentItem != null)
            {
                ReviewSubmitData review = this.CurrentItem as ReviewSubmitData;
                if (review != null)
                {
                    return !string.IsNullOrEmpty(review.SubmitUpgradePackage);
                }
                TaskSubmitData task = this.CurrentItem as TaskSubmitData;
                if (task != null)
                {
                    return !string.IsNullOrEmpty(task.SubmitUpgradePackage);
                }
            }
            return false;
        }

        //判断load的内容是否为空,true(有值)，false(空值)
        private bool GetEnableAnythingElse()
        {
            HistoryData emph = this.CurrentItem as HistoryData;
            if (emph != null)
            {
                return emph.AnythingElse;
            }
            ReviewSubmitData empr = this.CurrentItem as ReviewSubmitData;
            if (empr != null)
            {
                return empr.AnythingElse;
            }
            TaskSubmitData empt = this.CurrentItem as TaskSubmitData;
            if (empt != null)
            {
                return empt.AnythingElse;
            }

            //if (this.CurrentItem != null)
            //{
            //    HistoryData history = this.CurrentItem as HistoryData;
            //    if (history != null)
            //    {
            //        return !string.IsNullOrEmpty(history.SubmitAnythingElse);
            //    }

            //    ReviewSubmitData review = this.CurrentItem as ReviewSubmitData;
            //    if (review != null)
            //    {
            //        return !string.IsNullOrEmpty(review.SubmitAnythingElse);
            //    }

            //    TaskSubmitData task = this.CurrentItem as TaskSubmitData;
            //    if (task != null)
            //    {
            //        return !string.IsNullOrEmpty(task.SubmitAnythingElse);
            //    }
            //}

            return false;
        }

        /// <summary>
        /// 自测报告
        /// </summary>
        /// <returns></returns>
        private bool GetEnableSpectionReport()
        {
            HistoryData hsd = this.CurrentItem as HistoryData;
            if (hsd != null)
                return hsd.SpectionReport;
            return false;
        }

        private Guid GetDataContextGuid()
        {
            ReviewSubmitData rsd = this.CurrentItem as ReviewSubmitData;
            TaskSubmitData tsd = this.CurrentItem as TaskSubmitData;
            HistoryData hsd = this.CurrentItem as HistoryData;

            if (rsd != null)
                return rsd.FlowGuid;
            else if (tsd != null)
                return tsd.FlowGuid;
            else if (hsd != null)
                return hsd.FlowGuid;
            else
                return Guid.Empty;
        }
    }
}
